package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysRoleResource;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysRoleResourceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRoleResource 控制层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysRoleResourceController extends SystemBaseController {

    private final SysRoleResourceService sysRoleResourceService;

    @Autowired
    public SysRoleResourceController(SysRoleResourceService sysRoleResourceService) {
        this.sysRoleResourceService = sysRoleResourceService;
    }

    @ApiOperation(value = "根据id获取SysRoleResource")
    @ResponseBody
    @RequestMapping(value = "/getSysRoleResourceById.action", method = RequestMethod.POST)
    public HttpResult<SysRoleResource> getSysRoleResourceById() {
        HttpResult<SysRoleResource> hr = new HttpResult<SysRoleResource>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = (Long) map.get("id");
            super.checkEmpty(id, "请提供参数id");
            SysRoleResource sysRoleResource = sysRoleResourceService.getSysRoleResourceById(id);
            hr.setData(sysRoleResource);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存SysRoleResource")
    @ResponseBody
    @RequestMapping(value = "/saveSysRoleResource.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysRoleResource() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser sysUser = this.getSysUser();
            String json = JSON.toJSONString(map);
            SysRoleResource t = JSON.parseObject(json, SysRoleResource.class);
            sysRoleResourceService.saveSysRoleResource(t, sysUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除SysRoleResource")
    @ResponseBody
    @RequestMapping(value = "/deleteSysRoleResource.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysRoleResource() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String roleId = HlpUtils.getString(map, "roleId");
            String json = JSON.toJSONString(map.get("resourceIdList"));
            List<String> resourceIdList = JSON.parseArray(json, String.class);
            sysRoleResourceService.deleteSysRoleResource(opUser, roleId, resourceIdList);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询SysRoleResource")
    @ResponseBody
    @RequestMapping(value = "/findSysRoleResources.action", method = RequestMethod.POST)
    public HttpResult<List<SysRoleResource>> findSysRoleResources() {
        HttpResult<List<SysRoleResource>> hr = new HttpResult<List<SysRoleResource>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            //String searchValue = (String) map.get("searchValue");
            //if (!HlpUtils.isEmpty(searchValue)) {
            //	params.put("searchValue", "%" + searchValue + "%");
            //}
            PageBean page = new PageBean(map);
            List<SysRoleResource> list = sysRoleResourceService.findSysRoleResources(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取角色拥有的菜单")
    @ResponseBody
    @RequestMapping(value = "/findRoleResourcesByRoleId.action", method = RequestMethod.POST)
    public HttpResult<List<SysRoleResource>> findRoleResourcesByRoleId() throws Exception {
        HttpResult<List<SysRoleResource>> hr = new HttpResult<List<SysRoleResource>>();
        try {
            Map<String, Object> map = super.getParams(request);
            String roleId = HlpUtils.getString(map, "roleId");
            super.checkEmpty(roleId, "请提供参数roleId");
            List<SysRoleResource> list = sysRoleResourceService.findRoleResourcesByRoleId(roleId);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取多角色拥有的菜单")
    @ResponseBody
    @RequestMapping(value = "/findResource4RoleIds.action", method = RequestMethod.POST)
    public HttpResult<List<String>> findResource4RoleIds() throws Exception {
        HttpResult<List<String>> hr = new HttpResult<List<String>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Object obj = map.get("roleIds");
            super.checkEmpty(obj, "请提供参数roleIds");
            if (obj instanceof List) {
                List<String> ids = JSONArray.parseArray(JSON.toJSONString(obj), String.class);
                List<String> result = new ArrayList<>();
                if (ids.size() > 0) {
                    for (String sid : ids) {
                        List<SysRoleResource> list = sysRoleResourceService.findRoleResourcesByRoleId(sid);
                        if (!HlpUtils.isEmptyList(list)) {
                            for (SysRoleResource roleResource : list) {
                                if (!result.contains(roleResource.getResource_sid())) {
                                    result.add(roleResource.getResource_sid());
                                }
                            }
                        }
                    }
                }
                hr.setData(result);
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                throw new HlpException("数据类型不正确，非数组类型");
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
